|
|
|
|
|
|
|
You can create a library of procedures that can be used in more than one program, saving yourself development time in new projects. |
|
|
|
|
|
|
|
|
Program maintenance is easier for a couple of reasons. First, if code is not repeated, you have to edit it only once. In addition, separating key components (for example, the user interface and the database functions) allows you to make major changes in one part of the program without recoding the whole thing. |
|
|
|
|
| |
|
|
|
|
To make your code reusable, use comments often. This extra detail allows another programmer (or yourself after a long period of time) to see quickly the purpose of each procedure and how it accomplishes its task. |
|
|
|
|
|
|
|
|
|
|
As with your program as a whole, the process of creating a procedure starts with design. You need to determine the task to be performed , what information will be needed by the procedure, and what information will be returned by the procedure. After you complete this task, you can start the actual coding of the procedure. You can start building a procedure in Visual Basic in two ways: starting from scratch or using the Add Procedure dialog box. Both methods are relatively easy, and the one you use is a matter of personal preference. Also, both methods require you to be in the Code window before you can build a procedure. |
|
|
|
|
|
|
|
|
To create a procedure from scratch, place your cursor in the Code window in a location that is not within a currently defined function or procedure. For example, you might place the cursor after the End Sub statement of a procedure, before the Sub statement of another procedure, or at the top of the Code window. Figure 11.1 shows where you can start. |
|
|
|
|
|
|
|
|
Figure 11.1
Start a procedure in the Code window by using a Sub statement. |
|
|
|
|
| |
|
|
|
|
Placing your procedures in alphabetical order makes finding them easier when you page through the Code window. However, they will always be in alphabetical order in the Code window's drop-down Procedure box. |
|
|
|
|
|
|
|